package com.umeng.comm.ui.imagepicker.fragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.umeng.comm.core.beans.CommConfig;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.beans.ImageItem;
import com.umeng.comm.core.constants.Constants;
import com.umeng.comm.core.imageloader.cache.ImageCache;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.ui.imagepicker.adapters.FeedImageAdapter;
import com.umeng.comm.ui.imagepicker.adapters.UserAlbumAdapter;
import com.umeng.comm.ui.imagepicker.dialogs.ImageBrowser;
import com.umeng.comm.ui.imagepicker.mvpview.MvpAlbumView;
import com.umeng.comm.ui.imagepicker.presenter.impl.AlbumPresenter;
import com.umeng.comm.ui.imagepicker.presenter.impl.NullPresenter;
import com.umeng.comm.ui.imagepicker.util.ViewFinder;
import com.umeng.comm.ui.imagepicker.widgets.RefreshGvLayout;
import com.umeng.comm.ui.imagepicker.widgets.RefreshLayout;

import java.util.List;

/**
 * Created by summersoft on 2016/5/15.
 */
public class AlbumFragment extends BaseFragment<Void, NullPresenter> implements MvpAlbumView {

    RefreshGvLayout mRefreshGvLayout;
    FeedImageAdapter mImageAdapter;
    GridView mAlbumGridView;

    AlbumPresenter mPresenter;
    ImageBrowser mImageBrowser;

    private CommUser mUser;

    public AlbumFragment(){}

    public AlbumFragment(CommUser user){
        mUser = user;
    }

    public static AlbumFragment newInstance(CommUser user) {
        return new AlbumFragment(user);
    }


    @Override
    protected int getFragmentLayout() {
        return ResFinder.getLayout("umeng_commm_album_layout");
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getActivity().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        initTitleLayout();

        mImageBrowser = new ImageBrowser(getActivity());
        // 构建AlbumPresenter，参数1为用户id
//        mPresenter = new AlbumPresenter(getActivity().getIntent().getStringExtra(Constants.USER_ID_KEY), this);
        mPresenter = new AlbumPresenter(mUser.id, this);
        mPresenter.attach(getActivity().getApplicationContext());
    }

    private void initTitleLayout() {
//        ViewFinder viewFinder = new ViewFinder(getActivity().getWindow().getDecorView());

//        Button settingButton = viewFinder.findViewById(ResFinder.getId("umeng_comm_save_bt"));
        // 目前将设置功能移到发现页面，此时暂时隐藏设置按钮
//        settingButton.setVisibility(View.INVISIBLE);


//        findViewById(ResFinder.getId("umeng_comm_setting_back")).setOnClickListener(
//                new View.OnClickListener() {
//
//                    @Override
//                    public void onClick(View v) {
//                        getActivity().finish();
//                    }
//                });
//
//        TextView titleTextView = (TextView) findViewById(ResFinder
//                .getId("umeng_comm_setting_title"));
//        titleTextView.setText("我的图册");
        findViewById(ResFinder.getId("album_title")).setVisibility(View.GONE);


        // 初始化下拉刷新Layout
        mRefreshGvLayout = findViewById(ResFinder.getId("umeng_comm_album_swipe_layout"));
        mRefreshGvLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                mPresenter.loadDataFromServer();
            }
        });

        mRefreshGvLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                mPresenter.loadMore();
            }
        });

        // 初始化GridView
        mAlbumGridView =findViewById(ResFinder.getId("umeng_comm_user_albun_gv"));
        mAlbumGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                jumpToImageBrowser(mImageAdapter.getDataSource(), position);
            }
        });
        mImageAdapter = new UserAlbumAdapter(getActivity().getApplicationContext());
        mAlbumGridView.setAdapter(mImageAdapter);
    }

    private void jumpToImageBrowser(List<ImageItem> images, int position) {
        mImageBrowser.setImageList(images, position);
        mImageBrowser.show();
    }

    @Override
    public void fetchedAlbums(List<ImageItem> imageItems) {
        mImageAdapter.addData(imageItems);

        mRefreshGvLayout.setRefreshing(false);
        mRefreshGvLayout.setLoading(false);
    }

    @Override
    public List<ImageItem> getBindDataSource() {
        return mImageAdapter.getDataSource();
    }

    @Override
    public void onResume() {
        super.onResume();
        mRefreshGvLayout.setRefreshing(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ImageCache.getInstance().clearLruCache();
        mPresenter.onDestroy();
    }
}
